import librosa.display
import os
from moviepy.editor import VideoFileClip

def mp4_to_mp3(musicDir, input_mp4_file, output_mp3_file):
    if not os.path.exists(musicDir):
        os.mkdir(musicDir)
    # 打开MP4文件
    video_clip = VideoFileClip(input_mp4_file)
    # 提取音频
    audio_clip = video_clip.audio

    # 保存为MP3文件
    audio_clip.write_audiofile(output_mp3_file)
    # 关闭文件
    audio_clip.close()
    video_clip.close()

    print(f"MP3文件已生成: {output_mp3_file}")